<template>
  <div class="grid-content bg-purple">
    <el-table
      :data="BaseDataList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
    >
      <el-table-column prop="id" label="ID" sortable width="180">
      </el-table-column>
      <el-table-column prop="chr_name" label="名称" sortable> </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope)">
            修改
          </el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
var Enumerable = require("linq");
import { BaseDataList } from "@/api/basedata";
export default {
  components: { Pagination },
  data() {
    return {
      total: 0,
      BaseDataList: [],
    };
  },
  props: {
    listQuery: {
      type: Object,
      default: function () {
        return {
          page_size: 20,
          page_index: 1,
          p_type_id: "",
          p_type_code: "",
          condition: {
            chr_name: "",
          },
        };
      },
    },
  },
  mounted() {
    //
  },
  methods: {
    getList() {
      BaseDataList(this.listQuery).then((res) => {
        var list = res.data.list;
        var result = [];
        if (this.listQuery.condition.chr_name != "") {
          this.BaseDataList = list;
        } else {
          result = this.PaseData(result, list, 0);
          this.BaseDataList = result;
        }
        this.total = res.data.total;
      });
    },
    PaseData(result, data, parent_id) {
      var that = this;
      var check_modules_list = Enumerable.from(data)
        .where(function (item) {
          return item.parent_id == parent_id;
        })
        .toArray();
      check_modules_list.forEach((item, index) => {
        var temp_children = [];
        that.PaseData(temp_children, data, item.id);
        item["children"] = temp_children;
        result.push(item);
      });
      return result;
    },
  },

  watch: {
    listQuery: {
      //深度监听，可监听到对象、数组的变化
      handler(newV, oldV) {
        this.getList();
      },
      deep: true,
    },
  },
};
</script>

<style scoped>
</style>